package actions;

import java.awt.event.ActionEvent;

import client.ClientSocketConnection;
import client_gui.ClientWindow;

public class ChangeStatusAction extends RLMAbstractAction {

	
	public ChangeStatusAction(ClientWindow owner) {
		setActionOwner(owner);
//		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0));
//		putValue(SMALL_ICON, loadIcon("images/logout.png"));
		putValue(NAME, "Status");
		putValue(SHORT_DESCRIPTION, "Status");
	}
	
	
	public void actionPerformed(ActionEvent e) {
		
		ClientSocketConnection connection = getActionOwner().getApplicaton().retrieveSocketConnection();
		
		int status = getActionOwner().getNewStatus();
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("CHSTATUS ");
		buffer.append(getActionOwner().getApplicaton().getClientUser().getUsername());
		buffer.append(" ");
		buffer.append(STATUS_NAMES[status]);
		buffer.append(" ");
		buffer.append(getActionOwner().getApplicaton().getClientUser().getNickname());
		buffer.append(" ");
		buffer.append(getActionOwner().getApplicaton().getClientUser().getEmail());
		buffer.append(" ");
		buffer.append(getActionOwner().getApplicaton().getClientUser().getAvatar());
		
		String completedStatusInfo = buffer.toString();
		
		connection.setMessage(CHSTATUS_CMD, completedStatusInfo);
		
		String response;
		
		response = connection.buffers[CHSTATUS_CMD].read();
		
		if ( response.startsWith("100") ) {
			
			getActionOwner().setStatus(status);
			
			getActionOwner().getWindowInitialPanel().setInitialStatus(STATUS_NAMES[status]);
			
			getActionOwner().getApplicaton().getClientUser().setStatus(status);
			
			getActionOwner().setTrayIcon(status);
		}
	}
	
}
